package com.ytrsoft.base;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.setting.Setting;
import cn.hutool.setting.dialect.Props;
import com.formdev.flatlaf.FlatDarkLaf;
import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.ui.*;
import com.formdev.flatlaf.util.SystemInfo;
import com.sun.jna.Platform;
import com.ytrsoft.ui.AppPane;
import com.ytrsoft.util.Message;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.util.Map;
import java.util.Set;

public abstract class App extends JFrame {

    private static final float SCALE = 0.7F;
    private static final String TITLE = "歌词助手";

    public App() {
        initializeUI();
    }

    private void initializeUI() {
        setTitle(TITLE);
        setSize(getDefaultSize());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setupMacOSUI();
        setContentPane(new AppPane());
    }

    private Dimension getDefaultSize() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = (int) (screenSize.width * SCALE);
        int height = (int) (screenSize.height * SCALE);
        return new Dimension(width, height);
    }

    private void setupMacOSUI() {
        if (SystemInfo.isMacFullWindowContentSupported) {
            getRootPane().putClientProperty("apple.awt.transparentTitleBar", true);
        }
    }

    private static void configureForMacOS() {
        if (Platform.isMac()) {
            System.setProperty("apple.awt.application.name", TITLE);
            System.setProperty("apple.awt.application.appearance", "system");
        }
    }

    private static void setupLookAndFeel() {
        FlatArcBorder border = new FlatArcBorder();

        UIManager.put("Button.background", Color.decode("#3e3e3e"));
        UIManager.put("Button.pressedBackground", Color.decode("#3e3e3e"));
        UIManager.put("Button.focusedBackground", Color.decode("#3e3e3e"));
        UIManager.put("Button.foreground", Color.decode("#ffffff"));
        UIManager.put("Button.focusColor", Color.decode("#3e3e3e"));
        UIManager.put("Button.border", border);

        UIManager.put("TextField.background", Color.decode("#ffffff"));
        UIManager.put("TextField.foreground", Color.decode("#606266"));
        UIManager.put("TextField.border", border);
        UIManager.put("TextField.selectionBackground", Color.decode("#efeae8"));
        UIManager.put("TextField.caretForeground", Color.decode("#606266"));

        UIManager.put("ComboBox.selectionBackground", Color.decode("#efeae8"));
        UIManager.put("ComboBox.background", Color.decode("#ffffff"));
        UIManager.put("ComboBox.foreground", Color.decode("#606266"));
        UIManager.put("ComboBox.border", border);
        UIManager.put("ComboBox.popupBorder", BorderFactory.createEmptyBorder());

        UIManager.put("Label.foreground", Color.decode("#3e3e3e"));

        UIManager.put("Component.arc", 8);
        UIManager.put("Component.focusWidth", 0);
        UIManager.put("Component.innerFocusWidth", 0);

        UIManager.put("OptionPane.background", Color.decode("#efeae8"));
        UIManager.put("Panel.background", Color.decode("#efeae8"));

        UIManager.put("ProgressBar.foreground", Color.decode("#bfac78"));

        FlatLightLaf.setup();
    }

    private static void createAndShowApp(Class<? extends App> clz) {
        try {
            App app = clz.getDeclaredConstructor().newInstance();
            app.setVisible(true);
        } catch (Exception e) {
            Message.err(e);
        }
    }

    private static void setup(Class<? extends App> clz) {
        configureForMacOS();
        setupLookAndFeel();
        createAndShowApp(clz);
    }

    public static void launch(Class<? extends App> clz) {
        SwingUtilities.invokeLater(() -> setup(clz));
    }
}
